package com.atguigu.tingshu.album.api;

import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackOrderVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;


	//api/album/trackInfo/uploadTrack

	@Operation(summary = "上传声音文件")
	@PostMapping("uploadTrack")
	public Result uploadTrack(MultipartFile file){
		JSONObject jsonObject = vodService.uploadTrack(file);
		return Result.ok(jsonObject);
	}

	//api/album/trackInfo/saveTrackInfo
	@GuiguLogin
	@Operation(summary = "保存声音信息")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfo trackInfo){
		trackInfoService.saveTrackInfo(trackInfo);
		return Result.ok();
	}

	//api/album/trackInfo/findUserTrackPage/1/10
	@GuiguLogin
	@Operation(summary = "分页查询用户声音列表")
	@PostMapping("findUserTrackPage/{pageNum}/{pageSize}")
	public Result findUserTrackPage(@PathVariable Long pageNum,
	                                @PathVariable Long pageSize,
	                                @RequestBody TrackInfoQuery trackInfoQuery){
		Page<TrackListVo> trackListVoPage = trackInfoService.findUserTrackPage(pageNum, pageSize, trackInfoQuery);
		return  Result.ok(trackListVoPage);
	}

	//api/album/trackInfo/removeTrackInfo/51942
	@GuiguLogin
	@Operation(summary = "根据id删除声音信息")
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable("id") Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	//api/album/trackInfo/getTrackInfo/51933
	@GuiguLogin
	@Operation(summary = "根据id回显查询声音信息")
	@GetMapping("getTrackInfo/{id}")
	public Result getTrackInfo(@PathVariable("id") Long id){
		TrackInfo trackInfo = trackInfoService.getTrackInfo(id);
		return Result.ok(trackInfo);
	}

	//api/album/trackInfo/updateTrackInfo/51944
	@GuiguLogin
	@Operation(summary = "更新声音信息")
	@PutMapping("updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable("id") Long id,
	                              @RequestBody TrackInfo trackInfo){
		trackInfo.setId(id);
		trackInfoService.updateTrackInfo(id,trackInfo);
		return Result.ok();
	}

	//	/api/album/trackInfo/findAlbumTrackPage/761/1/10
	@GuiguLogin(required = false) //表示不需要登录
	@Operation(summary = "根据专辑id查询声音分页列表数据")
	@GetMapping("findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
	public Result findAlbumTrackPage(
			@PathVariable("albumId") Long albumId,
			@PathVariable("pageNum") Long pageNum,
			@PathVariable("pageSize") Long pageSize
	){
		//前端需要 得到声音的分页列表，  声音使用AlbumTrackListVo封装
		Page<AlbumTrackListVo> page =  trackInfoService
				.findAlbumTrackPage(albumId,pageNum,pageSize);
		return Result.ok(page);
	}



	//api/album/trackInfo/findUserTrackPaidList/4916
	/*
			[
				{"name":"本集" , "price": 0.3 , "trackCount": 0 },
				{"name":"后5集" , "price": 0.3*5 , "trackCount": 5 },
				{"name":"后所有集" , "price": 0.3*所有集数 , "trackCount": 集数 },
				//如果专辑是vip免费
				{"name":"全集" ,   "price":"VIP免费","trackCount": 0 }
			]
		 */
	@GuiguLogin
	@Operation(summary = "根据专辑Id，查询购买专辑的不同方式")
	@GetMapping("findUserTrackPaidList/{trackId}")
	//TrackOrderVo: 可以封装响应数据
	public Result findUserTrackPaidList(@PathVariable("trackId")Long trackId) {
		List<TrackOrderVo> trackOrderVoList = trackInfoService.findUserTrackPaidList(trackId);
		return Result.ok(trackOrderVoList);
	}


	@Operation(summary = "内部接口：根据声音id查询它和后面N个声音详情")
	@GetMapping("findTrackInfoList/{id}/{count}")
	public Result<List<TrackInfo>>findTrackInfoList(@PathVariable("id")Long id ,
													@PathVariable("count")Integer count ){
		TrackInfo trackInfo = trackInfoService.getById(id);
		if(count == 0){
			return Result.ok(Arrays.asList(trackInfo));
		}
		//select * from track_info where album_id = 1 and order_num>=1 order by order_bum limit 10;
		List<TrackInfo> trackInfos = trackInfoService.list(Wrappers.lambdaQuery(TrackInfo.class)
						.eq(TrackInfo::getAlbumId, trackInfo.getAlbumId())
						.ge(TrackInfo::getOrderNum, trackInfo.getOrderNum())
						.orderByAsc(TrackInfo::getOrderNum)
//						.notIn(TrackInfo::getId , xxx)
						.last(" limit " + count)
		);
		// 如果需要排除已购买的声音:
		return Result.ok(trackInfos);
	}

	@Operation(summary = "内部接口：根据声音id查询专辑id")
	@GuiguLogin
	@GetMapping("findAlbumIdByTrackId/{id}")
	public Result<Long> findAlbumIdByTrackId(@PathVariable("id")Long id){
		return Result.ok(trackInfoService.getById(id)
				.getAlbumId());
	}

	@Operation(summary = "内部接口：根据专辑id查询专辑下的所有声音id列表")
	@GuiguLogin
	@GetMapping("findTrackIdsByAlbumId/{albumId}")
	public Result<List<Long>> findTrackIdsByAlbumId(@PathVariable("albumId")Long albumId){
		return Result.ok(trackInfoService.list(Wrappers.lambdaQuery(TrackInfo.class)
						.eq(TrackInfo::getAlbumId , albumId))
				.stream().map(TrackInfo::getId).collect(Collectors.toList()));
	}

}

